package gui.dialog;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import main.Antiquity;

/**
 *	@(#)GameEndDialog.java
 *
 *	The <code>GameEndDialog</code> class asks a player to discard resources
 *	he cannot store due to a lack of enough storage capacity.
 *
 *	@author Grzegorz Kobiela
 *	@version 1.0 2008/4/26
 */
public final class GameEndDialog
extends JDialog
{
	/**
	 *	Creates a new <code>GameEndDialog</code> instance.
	 *	@param owner the owner frame
	 *	@param ranks the ranks
	 *	@param time the playing time
	 *	@param round the last round
	 */
	public GameEndDialog(final gui.GameWindow owner, main.Rank[] ranks, long time, int round) {
		super(owner, Antiquity.get("title"), false);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}
			}
		);

		/* Dialog window */
		Container contentPane = getContentPane();
		contentPane.setLayout(null);
		contentPane.setBounds((Rectangle)Antiquity.getProperty("GameEndDialogInnerBounds"));

		/* General message */
		JLabel label = new JLabel(
			String.format(
				Antiquity.get("dlgGameEndMessage"),
				getTime(time),
				Antiquity.get("dlgGameEnd" + (ranks[0].getPlayer().wins() ? "Regular" : "Preterm")),
				round
			)
		);
		label.setBounds((Rectangle)Antiquity.getProperty("GameEndDialogMessageBounds"));
		contentPane.add(label);

		/* Display ranks */
		Rectangle iconBounds = (Rectangle)Antiquity.getProperty("GameEndDialogIconBounds");
		Rectangle playerBounds = (Rectangle)Antiquity.getProperty("GameEndDialogPlayerBounds");
		Rectangle timeBounds = (Rectangle)Antiquity.getProperty("GameEndDialogTimeBounds");
		Rectangle roundBounds = (Rectangle)Antiquity.getProperty("GameEndDialogRoundBounds");
		Rectangle saintBounds = (Rectangle)Antiquity.getProperty("GameEndDialogSaintBounds");
		Rectangle sizeBounds = (Rectangle)Antiquity.getProperty("GameEndDialogSizeBounds");
		Rectangle panelBounds = (Rectangle)Antiquity.getProperty("GameEndDialogPanelBounds");
		int offset = (Integer)Antiquity.getProperty("GameEndDialogPanelOffset");
		for(main.Rank rank: ranks) {
			game.Player player = rank.getPlayer();
			JPanel panel = new JPanel(null);
			panel.setBounds(panelBounds);

			/* Display icon */
			Object type = (player.wins() ? "Flower" : (player.isActive() ? "Dot" : "Fire"));
			label = new JLabel(gui.GameUtilities.getIcon(type, player.getColor().toString()));
			label.setBounds(iconBounds);
			panel.add(label);

			/* Display player's rank and name */
			label = new JLabel(String.format("%d. %s", rank.getRank(), player.getName()));
			label.setBounds(playerBounds);
			panel.add(label);

			/* Display playing time */
			label = new JLabel(getTime(rank.getTime()));
			label.setBounds(timeBounds);
			panel.add(label);

			/* Display round */
			label = new JLabel(String.format("(%s %d)", Antiquity.get("round"), rank.getRound()));
			label.setBounds(roundBounds);
			panel.add(label);

			/* Display saint */
			label = new JLabel(gui.GameUtilities.getIcon("Saint", player.getSaint() != null ? player.getSaint().getType().toString() : "none"));
			label.setBounds(saintBounds);
			panel.add(label);

			/* Display ZOC */
			label = new JLabel(String.format("[%d %s]", rank.getSize(), Antiquity.get("space", rank.getSize())));
			label.setBounds(sizeBounds);
			panel.add(label);

			/* Add panel */
			contentPane.add(panel);
			panelBounds.translate(0, offset);
		}

		/* Replay buttons */
		JButton button = new JButton(Antiquity.get("dlgGameEndReplay"));
		button.setBounds((Rectangle)Antiquity.getProperty("GameEndDialogButton1Bounds"));
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Antiquity.reset();
				}
			}
		);
		contentPane.add(button);

		/* Quit button */
		button = new JButton(Antiquity.get("dlgGameEndQuit"));
		button.setBounds((Rectangle)Antiquity.getProperty("GameEndDialogButton2Bounds"));
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			}
		);
		contentPane.add(button);

		/* Dialog properties */
		Rectangle bounds = (Rectangle)Antiquity.getProperty("GameEndDialogBounds");
		setLocation(
			owner.getX() + (owner.getWidth() - bounds.width) / 2,
			owner.getY() + (owner.getHeight() - bounds.height) / 2
		);
		setSize(bounds.getSize());
		setResizable(false);
		setVisible(true);
	}

	/**
	 *	Returns the given time as a formatted string.
	 *	@return the given time as a formatted string
	 */
	private static String getTime(long time) {
		int s = (int)(time % 60); time /= 60;
		int m = (int)(time % 60); time /= 60;
		int h = (int)(time % 24); time /= 24;
		return (time > 0 ? Antiquity.get("dlgGameEndTime") : String.format("%02d:%02d:%02d", h, m, s));
	}
}